package org.huangrui.spark.java.core.rdd.serial;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * @Author hr
 * @Create 2024-10-18 11:56
 */
public class Spark01_RDD_Serial {
    public static void main(String[] args) {
        final SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);

        final List<Integer> nums = Arrays.asList(1,2,3,4);
        final JavaRDD<Integer> rdd = jsc.parallelize(nums, 2);

        // TODO 对象是在Driver端创建的
        Student s = new Student();

        // [1,2][3,4]
        // foreach算子是分布式循环：分区内有序，分区间无序
        // TODO
        //    SparkException: Task not serializable
        //    java.io.NotSerializableException: org.huangrui.spark.java.core.rdd.serial.Student
        rdd.foreach(
                num -> {
                    System.out.println(s.age + num);
                }
        );
        jsc.close();
    }
}
class Student implements Serializable {
    protected final Integer age = 18;
}
